package com.intelligentmeterreadingsys.waterlist.querywaterlist.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.LayoutInflater.Factory;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.GridView.GridMainActivity;
import com.intelligentmeterreadingsys.povo.UserWaterVO;
import com.intelligentmeterreadingsys.report.activity.ReportNoRFIDActivity;
import com.intelligentmeterreadingsys.waterbanding.activity.WaterBandingActivity;
import com.intelligentmeterreadingsys.waterlist.copywater.activity.CopyWaterNoRFIDActivity;
import com.intelligentmeterreadingsys.waterlist.db.WaterListDaoImpl;

/**
 * 查询抄表及水表信息
 * 
 * @author king
 * 
 */
public class WaterinfoActivity extends Activity {
    public static final int ITEM0 = Menu.FIRST;// 系统值
    public TextView textview1;// 用户姓名
    public TextView textview2;// 用户编号
    public TextView textview3;// 用户电话
    public TextView textview4;// 水表编号
    public TextView textview5;// 水表地址
    public TextView textview6;// 用水性质
    public TextView textview7;// 上次抄表时间
    public TextView textview8;// 上次抄表刻度
    public TextView textview9;// 本次抄表刻度
    public TextView textview10;// 抄表状态
    public UserWaterVO water = null;
    public String waterid = null;
    public Button button3;
    public QueryWaterListActivity act = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

	super.onCreate(savedInstanceState);
	setContentView(R.layout.waterinfo);

	inti();

    }

    /**
     * 初始化控件
     */
    public void inti() {
	textview1 = (TextView) findViewById(R.id.waternumber);
	textview2 = (TextView) findViewById(R.id.watervalue);
	textview3 = (TextView) findViewById(R.id.otherwatervalue);
	textview4 = (TextView) findViewById(R.id.lastwaternumber);
	textview5 = (TextView) findViewById(R.id.lastdate);
	textview6 = (TextView) findViewById(R.id.waterproperties);
	textview7 = (TextView) findViewById(R.id.wateradds);
	textview8 = (TextView) findViewById(R.id.tonnage);
	textview9 = (TextView) findViewById(R.id.waterlist_copystatus);
	textview10 = (TextView) findViewById(R.id.waterlist_user_phone);

    }

    /**
     * 添加数据
     * 
     * @param b
     * @param usernumber
     */
    public void addDate() {
	textview1.setText("");
	textview2.setText("");
	textview3.setText("");
	textview4.setText("");
	textview5.setText("");
	textview6.setText("");
	textview7.setText("");
	textview8.setText("");
	textview9.setText("");
	textview10.setText("");
	if (water != null) {
	    textview1.setText(water.getName());
	    if (water.getName() != null && water.getName().length() > 10) {
		textview1.setGravity(3);
	    }
	    textview2.setText(water.getNumber());
	    textview3.setText(water.getPhone());
	    textview4.setText(water.getWaternumber());
	    textview5.setText(water.getWateradds());
	    if (water.getWateradds() != null && water.getWateradds().length() > 10) {
		textview5.setGravity(3);
	    }
	    textview6.setText(water.getWaterproperties());
	    textview7.setText(water.getLastdate() + "");
	    textview8.setText(water.getLastmonthwater() + "");
	    if (water.getCopystatus().equals("已抄")) {
		textview9.setText(water.getThismonthwater() + "");
	    } else {
		textview9.setText("");
	    }

	    textview10.setText(water.getCopystatus());

	}

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

	menu.add(0, ITEM0, 0, "二次抄表");
	menu.add(0, ITEM0 + 1, 0, "百度地图");
	menu.add(0, ITEM0 + 2, 0, "图文绑定");
	menu.add(0, ITEM0 + 3, 0, "水表绑定");
	menu.add(0, ITEM0 + 4, 0, "RFID异常抄表");
	menu.add(0, ITEM0 + 5, 0, "RFID异常报案");
	menu.findItem(ITEM0 + 1);
	// 获取MenuInflater对象
	// setMenuBackGround();
	return true;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
	if (water == null) {
	    Toast.makeText(WaterinfoActivity.this, "没有水表不能进行些操作", Toast.LENGTH_LONG).show();
	    return false;
	}
	if (item.getItemId() == ITEM0) {
	    if (water.getCopystatus().equals("已抄")) {
		Toast.makeText(this, "已抄过的水表！", 1000).show();
		return false;
	    }
	    if (water.getIsupload().equals("1")) {
		Toast.makeText(this, "已上传了抄表数据！不能此操作", 1000).show();
		return false;
	    }
	    String number = textview4.getText().toString();
	    if (number != null && number.length() > 0) {
		WaterListDaoImpl dao = new WaterListDaoImpl(WaterinfoActivity.this);
		boolean b = dao.Modifytowcopy(number);
		if (b) {
		    Toast.makeText(WaterinfoActivity.this, "操作成功", Toast.LENGTH_LONG).show();
		    water = dao.queryOneWaterlist(water.getWaterid());

		    addDate();
		} else {
		    Toast.makeText(WaterinfoActivity.this, "操作失败", Toast.LENGTH_LONG).show();
		}
		dao.close();
	    } else {
		Toast.makeText(WaterinfoActivity.this, "没有水表编号不能进行些操作", Toast.LENGTH_LONG).show();

	    }

	} else if (item.getItemId() == (ITEM0 + 1)) {
	    Intent intent = new Intent(WaterinfoActivity.this, QueryRoutePlanMap.class);
	    if (water == null) {
		Toast.makeText(WaterinfoActivity.this, "没有水表不能进行些操作", Toast.LENGTH_LONG).show();
		return false;
	    }
	    WaterListDaoImpl dao = new WaterListDaoImpl(WaterinfoActivity.this);
	    water = dao.queryOneWaterlist(water.getWaterid());
	    dao.close();
	    if (water.getLatitude() > 0) {
		intent.putExtra("wateradds", water.getWateradds());
		intent.putExtra("latitude", water.getLatitude());
		intent.putExtra("longitude", water.getLongitude());
		intent.putExtra("adds", water.getLocationadds());
		startActivity(intent);
	    } else {
		Toast.makeText(WaterinfoActivity.this, "请先定位", Toast.LENGTH_SHORT).show();
	    }

	} else if (item.getItemId() == (ITEM0 + 2)) {
	    // AChartEngine ace = new AChartEngine();
	    // if (water == null) {
	    // Toast.makeText(WaterinfoActivity.this, "没有水表不能进行些操作",
	    // Toast.LENGTH_LONG).show();
	    // return false;
	    // }
	    // WaterListDaoImpl dao = new WaterListDaoImpl(this);
	    // List<UseWaterInfoPoVo> lists =
	    // dao.queryUseWater(water.getWaterid(), "0");
	    // dao.close();
	    // if (lists == null || lists.size() < 1) {
	    // Toast.makeText(WaterinfoActivity.this, "此水表没有历史用水量信息",
	    // Toast.LENGTH_LONG).show();
	    // return false;
	    // }
	    // Intent intent =
	    // ChartFactory.getBarChartIntent(WaterinfoActivity.this,
	    // ace.getBarDataset(lists), ace.getBarRenderer(lists),
	    // Type.DEFAULT);
	    // startActivity(intent);
	    Intent intent = new Intent(this, GridMainActivity.class);
	    intent.putExtra("waternumber", water.getWaternumber());
	    intent.putExtra("type", "1");
	    startActivity(intent);
	} else if (item.getItemId() == (ITEM0 + 3)) {
	    if (water == null) {
		Toast.makeText(WaterinfoActivity.this, "没有水表不能进行些操作", Toast.LENGTH_LONG).show();
		return false;
	    }
	    Intent intent = new Intent(WaterinfoActivity.this, WaterBandingActivity.class);
	    Bundle bundle = new Bundle();
	    bundle.putString("waterid", water.getWaterid());
	    intent.putExtras(bundle);
	    intent.putExtra("watreid", water.getWaterid());

	    startActivity(intent);
	} else if (item.getItemId() == (ITEM0 + 4)) {
	    if (water == null) {
		Toast.makeText(WaterinfoActivity.this, "没有水表不能进行些操作", Toast.LENGTH_LONG).show();
		return false;
	    }
	    Intent intent = new Intent(WaterinfoActivity.this, CopyWaterNoRFIDActivity.class);
	    intent.putExtra("waterid", water.getWaterid());
	    startActivity(intent);
	} else if (item.getItemId() == (ITEM0 + 5)) {
	    if (water == null) {
		Toast.makeText(WaterinfoActivity.this, "没有水表不能进行些操作", Toast.LENGTH_LONG).show();
		return false;
	    }
	    Intent intent = new Intent(WaterinfoActivity.this, ReportNoRFIDActivity.class);
	    intent.putExtra("waterid", water.getWaterid());
	    startActivity(intent);
	}
	return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {

	WaterListDaoImpl dao = new WaterListDaoImpl(this);
	act = (QueryWaterListActivity) getParent();
	water = dao.queryOneWaterlist(act.getWaterid());
	dao.close();
	addDate();
	super.onResume();
    }

    // 设置菜单背景颜色
    protected void setMenuBackGround() {
	getLayoutInflater().setFactory(new Factory() {

	    @Override
	    public View onCreateView(String name, Context context, AttributeSet attrs) {

		if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
		    LayoutInflater inflater = getLayoutInflater();
		    try {

			final View view = inflater.createView(name, null, attrs);
			new Handler().post(new Runnable() {

			    @Override
			    public void run() {
				System.out.println("name");
				view.setBackgroundColor(Color.GREEN);

			    }
			});
			return view;
		    } catch (InflateException e) {

			e.printStackTrace();
		    } catch (ClassNotFoundException e) {

			e.printStackTrace();
		    }

		}
		return null;

	    }
	});
    }

}
